package model.admin;

import ecom.beans.ProductStoreBean;
import ecom.beans.EcomAdminRemote;
import ecom.shell.ShellRemote;
import java.util.Collection;
import java.util.Map;
import model.AbstractPage;

/**
 *
 * @author Sebastien
 */
public class StoreManagement extends AbstractPage {

    private ProductStoreBean store;
    private Collection accounts;
    private int selectedAccount;
    private static final String xsl = "C://JAVA//wkspace//ecom//ecom-war//web//XSL/admin/store.xsl";
    private static final String formPage = "admin/store/form.jsp";
    private static final String listPage = "admin/list.jsp";

    public String addOrUpdate() throws Exception {
        String[] id = (String[]) getParameters().get("id");
        String[] requestAction = (String[]) getParameters().get("action");
        EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");

        if ((id != null && !"0".equalsIgnoreCase(id[0])) || (store != null && store.getReference() != 0)) {

            // update
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.updateProductStore(store.getReference(), selectedAccount, store.getName(), store.getCity());
            } // affichage du formulaire pré-rempli
            else {
                store = admin.getStoreById(Integer.valueOf(id[0]));
                accounts = admin.getAllAccounts();
                this.setPage(formPage);
                return SUCCESS;
            }
        } else {

            // insert
            if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
                admin.createProductStore(selectedAccount, store.getName(), store.getCity());
            } // affichage du formulaire pré-rempli
            else {
                accounts = admin.getAllAccounts();
                this.setPage(formPage);
                return SUCCESS;
            }
        }
        return list();
    }

    public String list() throws Exception {
        ShellRemote shell = (ShellRemote) getSession().get("shell");
        this.setDataToPrint(shell.executeCommand("store"));
        this.setXslPath(xsl);
        this.setPage(listPage);
        return SUCCESS;
    }

    public String delete() throws Exception {
        String[] id = (String[]) getParameters().get("id");

        if (id != null && !"".equalsIgnoreCase(id[0])) {
            EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");
            admin.removeStore(Integer.valueOf(id[0]));
        }
        return list();
    }

    @Override
    public void validate() {
        String[] requestAction = (String[]) getParameters().get("action");
        if (requestAction != null && "1".equalsIgnoreCase(requestAction[0])) {
            this.setPage(formPage);
            EcomAdminRemote admin = (EcomAdminRemote) getSession().get("adminRemote");
            accounts = admin.getAllAccounts();

            if (store.getName().length() == 0) {
                this.addFieldError("store.name", "Préciser le nom du magasin");
            }
            if (store.getCity().length() == 0) {
                this.addFieldError("store.city", "Préciser la ville du magasin");
            }
            if (selectedAccount == 0) {
                this.addFieldError("selectedAccount", "Préciser un compte");
            }
        }
    }

    @Override
    public String execute() throws Exception {
        message = "StoreManagement()";
        return SUCCESS;
    }

    public Collection getAccounts() {
        return accounts;
    }

    public void setSelectedAccount(int selectedAccount) {
        this.selectedAccount = selectedAccount;
    }

    public ProductStoreBean getStore() {
        return store;
    }

    public void setStore(ProductStoreBean store) {
        this.store = store;
    }
}